home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Tools / Win95 Secrets / SETUP.Z / LOADAPIS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-19  |  5.1 KB  |  175 lines

  1. //==================================
  2. // APISPY32 - Matt Pietrek 1995
  3. // FILE: LOADAPIS.C
  4. //==================================
  5. #include <windows.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <ctype.h>
  9. #include "intrcpt.h"
  10. #include "parmtype.h"
  11.  
  12. BOOL IsNewAPILine(PSTR pszInputLine);
  13. BOOL ParseNewAPILine(PSTR pszInput, PSTR pszDLLName, PSTR pszAPIName);
  14. PARAMTYPE GetParameterEncoding(PSTR pszParam);
  15. PSTR SkipWhitespace(PSTR pszInputLine);
  16.  
  17. extern HINSTANCE HInstance;
  18.  
  19. BOOL LoadAPIConfigFile(void)
  20. {
  21.     FILE *pFile;
  22.     char szInput[256];
  23.     BYTE params[33];
  24.     BOOL fBuilding = FALSE;
  25.     char szAPIFunctionFile[MAX_PATH];
  26.     PSTR p;
  27.  
  28.     // Create a string with the path to the API function file.  This
  29.     // file will be in the same directory as this DLL
  30.     GetModuleFileName(HInstance, szAPIFunctionFile, sizeof(szAPIFunctionFile));
  31.     p = strrchr(szAPIFunctionFile, '\\')+1;
  32.     strcpy(p, "APISPY32.API");
  33.  
  34.     pFile = fopen(szAPIFunctionFile, "rt");
  35.     if ( !pFile )
  36.         return FALSE;
  37.     
  38.     //
  39.     // Format of a line is moduleName:APIName
  40.     // (e.g., "KERNEL32.DLL:LoadLibraryA")
  41.     //
  42.     while ( fgets(szInput, sizeof(szInput), pFile) )
  43.     {
  44.         PSTR pszNewline, pszInput;
  45.         char szAPIName[128], szDLLName[128];
  46.  
  47.         pszInput = SkipWhitespace(szInput);
  48.         
  49.         if ( *pszInput == '\n' )    // Go to next line if this line is blank
  50.             continue;
  51.         
  52.         pszNewline = strrchr(pszInput, '\n');   // Look for the newline
  53.         if ( pszNewline )
  54.             *pszNewline = 0;                    // Hack off the newline
  55.  
  56.         if ( IsNewAPILine(pszInput) )
  57.         {
  58.             // Dispense with the old one we've been building
  59.             if ( fBuilding )
  60.                 AddAPIFunction(szDLLName, szAPIName, params);
  61.  
  62.             if ( ParseNewAPILine(pszInput, szDLLName, szAPIName) ) 
  63.                 fBuilding = TRUE;
  64.             else
  65.                 fBuilding = FALSE;
  66.             
  67.             params[0] = 0;  // New set of parameters
  68.         }
  69.         else    // A parameter line
  70.         {
  71.             BYTE param = (BYTE)GetParameterEncoding(pszInput);
  72.             if ( param != PARAM_NONE )
  73.             {
  74.                 params[ params[0] +1 ] = param; // Add param to end of list
  75.                 params[0]++;                    // Update the param count
  76.             }
  77.             else
  78.             {
  79.                 if ( (*pszInput != 0) && (stricmp(pszInput, "VOID") != 0) )
  80.                 {
  81.                     char errBuff[256];
  82.                     wsprintf(errBuff, "Unknown param %s in %s\r\n",
  83.                             pszInput, szAPIName);
  84.                     OutputDebugString(errBuff);
  85.                 }
  86.             }
  87.         }
  88.     }
  89.  
  90.     fclose( pFile );
  91.     
  92.     return TRUE;
  93. }
  94.  
  95.  
  96. // Returns TRUE if this line is the start of an API definition.  It assumes
  97. // that any whitespace has already been skipped over.
  98. BOOL IsNewAPILine(PSTR pszInputLine)
  99. {
  100.     return 0 == strnicmp(pszInputLine, "API:", 4);
  101. }
  102.  
  103. // Break apart a function definition line into a module name and a function
  104. // name.  Returns those strings in the passed PSTR buffers.
  105. BOOL ParseNewAPILine(PSTR pszInput, PSTR pszDLLName, PSTR pszAPIName)
  106. {
  107.     PSTR pszColonSeparator;
  108.  
  109.     pszDLLName[0] = pszAPIName[0] = 0;
  110.     
  111.     pszInput += 4;  // Skip over "API:"
  112.  
  113.     pszColonSeparator = strchr(pszInput, ':');
  114.     if ( !pszColonSeparator )
  115.         return FALSE;
  116.     
  117.     *pszColonSeparator++ = 0;   // Null terminate module name, bump up
  118.                                 // pointer to API name
  119.  
  120.     strcpy(pszDLLName, pszInput);
  121.     strcpy(pszAPIName, pszColonSeparator);
  122.     
  123.     return TRUE;
  124. }
  125.  
  126.  
  127. typedef struct tagPARAM_ENCODING
  128. {
  129.     PSTR        pszName;    // Parameter name as it appears in APISPY32.API
  130.     PARAMTYPE   value;      // Associated PARAM_xxx enum from PARMTYPE.H
  131. } PARAM_ENCODING, * PPARAM_ENCODING;
  132.  
  133. PARAM_ENCODING ParamEncodings[] = 
  134. {
  135. {"DWORD", PARAM_DWORD},
  136. {"WORD", PARAM_WORD},
  137. {"BYTE", PARAM_BYTE},
  138. {"LPSTR", PARAM_LPSTR},
  139. {"LPWSTR", PARAM_LPWSTR},
  140. {"LPDATA", PARAM_LPDATA},
  141. {"HANDLE", PARAM_HANDLE},
  142. {"HWND", PARAM_HWND},
  143. {"BOOL", PARAM_BOOL},
  144. {"LPCODE", PARAM_LPCODE},
  145. };
  146.  
  147. // Given a line that's possibly a parameter line, returns the PARAM_xxx
  148. // encoding for that parameter type.  Lines that don't match any of the
  149. // strings in the ParamEncodings cause the function to return PARAM_NONE.
  150. PARAMTYPE GetParameterEncoding(PSTR pszParam)
  151. {
  152.     unsigned i;
  153.     PPARAM_ENCODING pParamEncoding = ParamEncodings;
  154.         
  155.     for ( i=0; i < (sizeof(ParamEncodings)/sizeof(PARAM_ENCODING)); i++ )
  156.     {
  157.         if ( stricmp(pParamEncoding->pszName, pszParam) == 0 )
  158.             return pParamEncoding->value;
  159.         
  160.         pParamEncoding++;
  161.     }
  162.  
  163.     return PARAM_NONE;
  164. }
  165.  
  166. // Given a pointer to an ASCIIZ string, return a pointer to the first
  167. // non-whitespace character in the line.
  168. PSTR SkipWhitespace(PSTR pszInputLine)
  169. {
  170.     while ( *pszInputLine && isspace(*pszInputLine) )
  171.         pszInputLine++;
  172.     return pszInputLine;
  173. }
  174.  
  175.